<!--
 * @Author: 高瑞寒 15932717091@163.com
 * @Date: 2022-04-25 21:35:52
 * @LastEditTime: 2022-04-27 12:01:05
 * @LastEditors: 高瑞寒 15932717091@163.com
 * @Description: 
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title></title>
  <style>
    .box {
      width: 200px;
      height: 100px;
      overflow: auto;
      background: red;
    }
  </style>
</head>

<body>
  <div id="test"></div>
  <script type="text/javascript" src="../js/新/react.development.js"></script>
  <script type="text/javascript" src="../js/新/react-dom.development.js"></script>
  <script type="text/javascript" src="../js/新/babel.min.js">
  </script>
  <script src="../js/新/prop-types.js"></script>
  <script type="text/babel">
    class List extends React.Component {
      state = { list: [] }
      getSnapshotBeforeUpdate(preProps, preState) {
        const height = this.listRef.scrollHeight
        return height
      }
      componentDidUpdate(preProps, preState, height) {
        this.listRef.scrollTop += this.listRef.scrollHeight - height
      }
      componentDidMount() {
        setInterval(() => {
          this.setState({
            list: ['消息' + (this.state.list.length + 1), ...this.state.list]
          })
        }, 1000)
      }
      render() {
        return (
          <div className='box' ref={e => this.listRef = e}>
            {this.state.list.map((item, index) => <div key={index}>{item}</div>)}
          </div>
        )
      }
    }
    ReactDOM.render(<div><List /></div>, document.querySelector('#test'))
  </script>
</body>

</html>